package cn.edu.jlu.community.disuse;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.GetCallback;
import com.avos.avoscloud.SaveCallback;

import java.util.List;

import cn.edu.jlu.community.R;
import it.neokree.materialnavigationdrawer.MaterialNavigationDrawer;

public class PostContentActivity extends MaterialNavigationDrawer {

    //具体POST内容页面

    @Override
    public void init(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_post_content);

        final Intent intent = getIntent();

        StringBuffer sb = new StringBuffer();
        sb.append("title:" + intent.getStringExtra("title") + "\n")
                .append("write by:" + intent.getStringExtra("author") + "\n\n")
                .append(intent.getStringExtra("content") + "\n\n");

        TextView tv = (TextView) findViewById(R.id.post_content_text_view);
        tv.setText(sb.toString());

        //show comments
        AVQuery<AVObject> query_for_post = new AVQuery<AVObject>("Post");
        query_for_post.getInBackground(intent.getStringExtra("objID"), new GetCallback<AVObject>() {
            @Override
            public void done(AVObject tempPost, AVException e) {
                if (e == null) {
                    Log.i("aaa", "get post by id in background ok ");
                    AVQuery<AVObject> query_for_comment = AVQuery.getQuery("Comment");
                    query_for_comment.whereEqualTo("post", tempPost);
                    query_for_comment.findInBackground(new FindCallback<AVObject>() {
                        public void done(List<AVObject> commentList, AVException e) {
                            // commentList now has the comments for myPost
                            if (e == null) {
                                Log.i("aaa", "query for comment in background ok , comment count: " + commentList.size());
                                TextView tv = (TextView) findViewById(R.id.post_content_text_view);
                                tv.append("COMMENTS :\n");
                                for (AVObject comment : commentList) {
                                    tv.append((String) comment.get("content") + "\n");
                                    tv.append("    --- by " + (String) comment.get("author") + "\n\n");
                                }
                            } else {
                                Log.i("aaa", "query for comment in background failed ");
                            }
                        }
                    });
                } else {
                    Log.i("aaa", "get post by id in background failed " + e.getMessage());
                }
            }
        });
        ;


        Button commentButton = (Button) findViewById(R.id.post_content_comment_button);
        commentButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText ed = (EditText) findViewById(R.id.post_content_comment_edit_text);
                String content = ed.getText().toString();
                comment(content, intent.getStringExtra("objID"));
            }
        });
    }

    private void comment(String content, final String postID) {
        AVObject myComment = new AVObject("Comment");
        myComment.put("content", content);
        myComment.put("author", AVUser.getCurrentUser().getUsername());
        myComment.put("post", AVObject.createWithoutData("Post", postID));  //数据关联
        myComment.saveInBackground(new SaveCallback() {
            @Override
            public void done(AVException e) {
                if (e == null) {
                    AVQuery<AVObject> query = new AVQuery<AVObject>("Post");
                    query.whereEqualTo("objectId", postID);
                    query.findInBackground(new FindCallback<AVObject>() {
                        @Override
                        public void done(List<AVObject> posts, AVException e) {
                            if (e == null) {
                                posts.get(0).increment("commentCount");
                                posts.get(0).saveInBackground();

                                Log.i("aaa", "comment test succeeded");
                            } else {
                                Log.i("aaa", "comment -- get post by obj id failed :" + e.getMessage());
                            }
                        }
                    });
                    Toast.makeText(PostContentActivity.this, "comment complete", Toast.LENGTH_LONG).show();
                } else {
                    Log.i("aaa", "comment test failed , something wrong " + e.getMessage());
                    Toast.makeText(PostContentActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        });
    }

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        // Inflate the menu; this adds items to the action bar if it is present.
//        getMenuInflater().inflate(R.menu.menu_post_content, menu);
//        return true;
//    }
//
//    @Override
//    public boolean onOptionsItemSelected(MenuItem item) {
//        // Handle action bar item clicks here. The action bar will
//        // automatically handle clicks on the Home/Up button, so long
//        // as you specify a parent activity in AndroidManifest.xml.
//        int id = item.getItemId();
//
//        //noinspection SimplifiableIfStatement
//        if (id == R.id.action_settings) {
//            return true;
//        }
//
//        return super.onOptionsItemSelected(item);
//    }
}
